const app = getApp()
import { axios } from "../../utils/request"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    initData: {} as Room,
    comment: [] as any,
    startTime: "",
    endTime: "",
    days: 1,
  },
  dateInit() {
    let { start, end } = app.globData
    let startTime = start.format("M月D日")
    let endTime = end.format("M月D日")
    let days = end.diff(start, "day")
    let price = this.data.initData.price * days
    app.globData.roomOrder.price = price
    app.globData.roomOrder.info = this.data.initData
    this.setData({
      startTime,
      endTime,
      days,
    })
  },
  toConfirm() {
    const isLogin = wx.getStorageSync("isLogin")
    let { start, end } = app.globData
    let days = end.diff(start, "day")
    let price = this.data.initData.price * days
    app.globData.roomOrder.price = price
    app.globData.roomOrder.info = this.data.initData
    if (isLogin) {
      wx.navigateTo({
        url: "/pages/confirm/confirm",
      })
    } else {
      wx.navigateTo({ url: "/pages/login/login" })
    }
  },
  add() {
    let { end } = app.globData
    app.globData.end = end.add(1, "day")
    this.dateInit()
  },
  dec() {
    if (this.data.days > 1) {
      let { end } = app.globData
      app.globData.end = end.subtract(1, "day")
      this.dateInit()
    }
  },
  //收藏房间
  async start() {
    const isLogin = wx.getStorageSync("isLogin")
    if (isLogin) {
      let roomId = this.data.initData.id
      ;(await axios("/start", "POST", {
        roomId,
      })) as Response
      let initData = this.data.initData
      let isLike = this.data.initData.isLike as boolean
      initData.isLike = !isLike
      this.setData({
        initData,
      })
    } else {
      wx.showToast({
        title: "请先登录",
        icon: "none",
      })
    }
  },
  //取消收藏
  async cancelStart() {
    let roomId = this.data.initData.id
    ;(await axios("/start/cancel", "POST", {
      roomId,
    })) as Response
    let initData = this.data.initData
    let isLike = this.data.initData.isLike as boolean
    initData.isLike = !isLike
    this.setData({
      initData,
    })
  },
  //跳转评论页面
  toComment() {
    let comment = this.data.comment
    wx.navigateTo({
      url: "/pages/roomComment/roomComment",
      success: (res) => {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit("context", comment)
      },
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad() {
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on("context", (data) => {
      this.setData({
        initData: data,
      })
    })
    let roomId = this.data.initData.id
    let commentData = (await axios(`/comment/${roomId}`)) as Response
    let comment = commentData.data.map((item) => {
      item.createTime = item.createTime.substring(0, 10)
      return item
    })
    this.setData({
      comment,
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.showLoading({
      title: "加载中",
      mask: true,
    })
    this.dateInit()
    setTimeout(function () {
      wx.hideLoading()
    }, 400)
  },
})
